﻿using System.Collections.Generic;
using System.ComponentModel;

namespace BuildARoute.Models
{
    /// <summary>
    /// This was taken from www.e-pedro.com (Pedro Sampaio's blog)
    /// </summary>
    public class ViewModelBase : INotifyPropertyChanged
    {
        private readonly Dictionary<string, PropertyChangedEventArgs> eventArgsCache;

        protected ViewModelBase()
        {
            eventArgsCache = new Dictionary<string, PropertyChangedEventArgs>();
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventArgs args;
            if (!eventArgsCache.TryGetValue(propertyName, out args))
            {
                args = new PropertyChangedEventArgs(propertyName);
                eventArgsCache.Add(propertyName, args);
            }

            RaisePropertyChanged(args);
        }

        protected void RaisePropertyChanged(PropertyChangedEventArgs args)
        {
            var handler = PropertyChanged;
            if (handler != null)
                handler(this, args);
        }

        #endregion
    }
}
